<template>
  <component is="a-modal" v-model:visible="visible" :width="800" :title="title" :mask-closable="false"
    :ok-loading="loading" @cancel="close" @before-ok="submit">
    <!-- 表单信息 start -->
    <a-form ref="formRef" :model="formData" :rules="rules" :auto-label-width="true">
      <a-form-item label="语言" field="lang_id">
        <sa-radio :valueKey="'id'" :titleKey="'title'" :data="paramsData.lang" v-model="formData.lang_id"
          placeholder="请选择"></sa-radio>
      </a-form-item>
      <a-form-item label="名称" field="name">
        <a-input v-model="formData.name" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="简述" field="desc">
        <a-textarea v-model="formData.desc" placeholder="请输入"></a-textarea>
      </a-form-item>
      <a-form-item label="标签" field="lable">
        <a-input v-model="formData.lable" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="详情" field="details">
        <ma-wangEditor v-model="formData.details"></ma-wangEditor>
      </a-form-item>
     


    </a-form>
    <!-- 表单信息 end -->
  </component>
</template>

<script setup>
import { ref, reactive, computed, watch, nextTick } from 'vue'
import { Message } from '@arco-design/web-vue'
import SaRadio from '@cps/sa-radio/index.vue'
import MaWangEditor from '@cps/ma-wangEditor/index.vue'
import mallApi from '@/api/system/mall'
import ENUMS from '@/utils/enums'
import tool from '@/utils/tool';
import { throttle } from 'lodash'
const paramsData = ref([])
const getParams = async (id) => {
  const { data } = await mallApi.add_goods_language_params(id)
  paramsData.value = data
  console.log(1, paramsData.value);
}
const emit = defineEmits(['success'])
// 引用定义
const formRef = ref()
const mode = ref('')
const visible = ref(false)
const loading = ref(false)
let title = computed(() => {
  return (mode.value == 'add' ? '新增' : '编辑')
})
// 表单信息
const formData = reactive({
  lang_id: '',
  name: '',
  id: null,
  main_id: null,
  desc: '',
  lable: '',
  details: '',
})
// 验证规则
const rules = ref({
  lang_id: [{ required: true, message: '请输入' }],
  name: [{ required: true, message: '请输入' }],
  lable: [{ required: true, message: '请输入' }],
  desc: [{ required: true, message: '请输入' }],
  details: [{ required: true, message: '请输入' }],

});
// 打开弹框
const open = async (type = 'add', main_id, id = '') => {
  mode.value = type
  visible.value = true
  formRef.value.resetFields()
  formData.main_id = main_id
  await initPage()
  await getParams(main_id)
  if (type == 'add') {
  } else {
    formData.id = id
  }

}
// 初始化页面数据
const initPage = async () => {
}

// 设置数据
const setFormData = async (data) => {
  for (const key in formData) {
    if (data[key] != null && data[key] != undefined) {
      formData[key] = data[key]
    }
  }
  console.log('111', formData);
}

// 数据保存
const submit = throttle(async (done) => {
  const validate = await formRef.value?.validate()
  if (!validate) {
    loading.value = true
    try {
      let data = { ...formData }
      let result = {}
      if (mode.value === 'add') {
        // 添加数据
        data.id = ''
        result = await mallApi.add_goods_language(data)
      } else {
        // 修改数据
        result = await mallApi.update_goods_language(data)
      }
      if (result.code === 200) {
        Message.success('操作成功')
        emit('success')
        done(true)
      }
      // 防止连续点击提交
      setTimeout(() => {
        loading.value = false
      }, 500)
    } catch {
      loading.value = false
    }
  }
  done(false)
}, 100)

// 关闭弹窗
const close = () => (visible.value = false)

defineExpose({ open, setFormData })
</script>